

clear all
set more off
capture log close
set mat 5000

cd "C:\Users\rujia\Dropbox\coalmine\restat"

use  CoalMine.dta, clear
drop if native==.
drop if leader_id==.

gen ln_output = log(prod0)

gen prod0_normalized=prod0/10000
label var prod0_normalized "Output"


label var ln_output "ln Output"


bysort prov_id: egen pre_deathrate=mean(deathrate) if year<1998
bysort prov_id: egen m_pre_deathrate=mean(pre_deathrate) 
gen pre_year=m_pre_deathrate*year
gen decentralized=(year>=1998 & year<=2000)

tabu prov_id, gen (pdummy)
forvalues i=1(1)22{
gen prov_year`i'=pdummy`i'*year
}

gen period=1 if year<1998
replace period=2 if year>=1998 & year<=2000
replace period=3 if year>2000 & year<=2005

egen clusterg=group(prov_id period)

gen decentralized_native=decentralized*native

gen ln_wage=log(indwage)
gen tenure=year-startyear+1
gen ln_gdpper=log(gdpper)
gen ln_electricity=log(eleconsum)
gen ln_disbeijing=log(1+dis_beijing)





*drop _m
merge 1:1 prov_id year  using TrafficPanel.dta

drop _m

merge 1:1 prov_id year using LocalCoalmine.dta

drop _m
merge 1:1 prov_id year using Population.dta



gen trafficper=mortality*10/popu



gen native_decentralized=native*decentralized
label var native_decentralized "Decentralization * Native"
label var native "Native"

drop _m

merge 1:m prov_id year using keycoalfirm.dta
drop _merge



drop if id==.

tabu id, gen(fdummy)




gen ln_wageper2=log(wage/worker)
gen ln_welfareper=log(welfare/worker)
label var ln_welfareper "Welfare exp. per worker"

drop if id==.


label var ln_wageper2 "ln Wage"

gen ln_firmoutput=log(output)
drop if ln_firmoutput==.
drop if native==.
label var ln_firmoutput "ln Output"


gen ln_worker=log(worker)

label var ln_worker "ln Employment"

local controls "tenure age ln_gdpper  gov_native s_native ln_disbeijing ln_electricity "

foreach var in tenure age ln_gdpper ln_electricity gov_native s_native ln_disbeijing {
gen decentralized_`var'=decentralized*`var'
}




#delimit ;
/* Clear any regression results kept in memory */
eststo clear;


eststo: xi:areg ln_firmoutput native_decentralized  i.prov_id i.year i.leader_id, absorb(id) cluster (clusterg);
estadd local pyfe "Y";
estadd local rfe "Y";
estadd local ife "Y";
estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);

eststo: xi:areg ln_firmoutput native_decentralized  `controls' decentralized_*  i.prov_id i.year i.leader_id, absorb(id) cluster (clusterg);
estadd local pyfe "Y";
estadd local rfe "Y";
estadd local ife "Y";
estadd local controls "Y";
estadd local controls_d "Y";
estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);


eststo: xi:areg ln_worker native_decentralized  i.prov_id i.year i.leader_id, absorb(id) cluster (clusterg);
estadd local pyfe "Y";
estadd local rfe "Y";
estadd local ife "Y";

estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);


eststo: xi:areg ln_worker native_decentralized  `controls' decentralized_*  i.prov_id i.year i.leader_id, absorb(id) cluster (clusterg);
estadd local pyfe "Y";
estadd local rfe "Y";
estadd local ife "Y";
estadd local controls "Y";
estadd local controls_d "Y";
estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);


eststo: xi:areg ln_wageper2 native_decentralized  i.prov_id i.year i.leader_id, absorb(id) cluster (clusterg);
estadd local pyfe "Y";
estadd local rfe "Y";
estadd local ife "Y";

estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);


eststo: xi:areg ln_wageper2 native_decentralized  `controls' decentralized_*  i.prov_id i.year i.leader_id, absorb(id) cluster (clusterg);
estadd local pyfe "Y";
estadd local rfe "Y";
estadd local ife "Y";
estadd local controls "Y";
estadd local controls_d "Y";
estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);


eststo: xi:areg ln_welfareper native_decentralized  i.prov_id i.year i.leader_id, absorb(id) cluster (clusterg);

estadd local pyfe "Y";
estadd local rfe "Y";
estadd local ife "Y";

estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);


eststo: xi:areg ln_welfareper native_decentralized  `controls' decentralized_*  i.prov_id i.year i.leader_id, absorb(id) cluster (clusterg);
estadd local pyfe "Y";
estadd local rfe "Y";
estadd local ife "Y";
estadd local controls "Y";
estadd local controls_d "Y";
estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);

esttab using draft/CoalFirms.tex, replace
	width(\hsize) 
    mlabel (, depvar)
	b(3)
	se(3)
	noconstant
	star(* 0.10 ** 0.05 *** 0.01)
	keep(
 native_decentralized

		)
	label
	stats(
		pyfe 
		rfe
		ife
		controls
		controls_d
		nobs
		rsq
		, 
		labels(
			"Province and Year FE" 
			"Regulator FE"
			"Year FE"
			"Controls"
			"Decentralization * Controls"
			"\# observations"
			"R-Squared"
			) 
		fmt(0 0 0 0 0 0  2)
		layout(@)
		)
	nonotes
	addnotes(
	)
	;
